package com.swgk.core.view.weiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.swgk.core.R;
import com.swgk.core.view.adapter.TopViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : younghow
 * @date : 2021/11/19 10:36
 * description:
 */
public class TopView<D> extends RelativeLayout {

    RecyclerView mRvTeamTop;
    RelativeLayout mRlExpand;
    ImageView mIvExpand;
    private TopViewAdapter<D> mTopViewAdapter;
    private List<D> mData = new ArrayList<>();
    private TopViewOfInter<D> mTopViewOfInter;
    private boolean isExpand;

    public void setTopViewOfInter(TopViewOfInter<D> mTopViewOfInter) {
        this.mTopViewOfInter = mTopViewOfInter;
    }

    public TopView(Context context) {
        super(context);

        init(context, null);
    }

    public TopView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            try {
                LayoutInflater.from(context).inflate(R.layout.library_view_team_top, this);

                mRvTeamTop = findViewById(R.id.rv_team_top);
                mRlExpand = findViewById(R.id.rl_expand);
                mIvExpand = findViewById(R.id.iv_expand);

                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
                mRvTeamTop.setLayoutManager(linearLayoutManager);
                mTopViewAdapter = new TopViewAdapter<>(context, null);
                mRvTeamTop.setAdapter(mTopViewAdapter);

                mRlExpand.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        closeExpand(!isExpand);
                    }
                });

                mTopViewAdapter.setOnItemClick(new TopViewAdapter.ItemClick<D>() {
                    @Override
                    public void itemClick(D drawable, List<D> list, int parentPosition, int childPosition) {
                        mTopViewOfInter.itemClick(drawable,list,parentPosition, childPosition);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public RecyclerView getRecyclerView() {
        return mRvTeamTop;
    }

    public void setData(List<D> list) {
        this.mData = list;
        int page = (int) Math.ceil(list.size()/6.0);
        List<List<D>> oneLevel = new ArrayList<>();
        for (int i = 0; i < page; i++) {
            int fir = i * 6;
            int last = Math.min(i * 6 + 6, list.size());
            List<D> menuEntityList = new ArrayList<>(list.subList(fir, last));
            oneLevel.add(menuEntityList);
        }
        mTopViewAdapter.setDatas(oneLevel);
        mTopViewAdapter.setDefItem(0);
    }

    public TopViewAdapter<D> getAdapter() {
        return mTopViewAdapter;
    }

    public List<D> getData() {
        return mData;
    }

    public void setCheckedPosition(int position) {
        if (mData != null && mData.size() > 0) {
            mTopViewAdapter.setDefItem(position);
        }
    }

    public interface TopViewOfInter<D> {
        default void imageClick() {

        }

        default void itemClick(D s, List<D> parent,int position,int childPosition) {

        }

        default void refresh() {

        }
    }

    public void closeExpand(boolean expand) {
        isExpand = expand;
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        if (!expand) {
            linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
            mRvTeamTop.setLayoutManager(linearLayoutManager);
            mRvTeamTop.setAdapter(mTopViewAdapter);
            mIvExpand.setBackgroundResource(R.mipmap.packers_team_expand_down);
        } else {
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            mRvTeamTop.setLayoutManager(linearLayoutManager);
            mRvTeamTop.setAdapter(mTopViewAdapter);
            mIvExpand.setBackgroundResource(R.mipmap.goods_icon_menu_up);
        }
    }
}
